import store from '../store/index'
const {
  requestDomain
} = store.data
export default class UploadFactory {
  constructor() {}
  /**
   * 上传文件 
   * @param {*} files 文件列表
   * @param {*} dir 文件上传目录
   */
  static upLoadFile(files, dir) {
    const promises = files.map(item => {
      return new Promise((resolve, reject) => {
        wx.uploadFile({
          filePath: item,
          name: 'file',
          url: `https://${requestDomain}/AppMaster/Tool/uploadFile`,
          formData: {
            save_dir: dir
          },
          success: res => {
            const {
              data
            } = res
            const result = JSON.parse(data)
            const {
              this_img_uri
            } = result
            resolve(this_img_uri)
          },
          fail: error => {
            reject(error)
          }
        })
      })
    })
    return Promise.all(promises).then(res => {
      return res
    }).catch(() => {
      return null
    })
  }
}